package com.fz.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.common.GlobalAll;
import com.fz.common.ResponseEntity;
import com.fz.dto.system.dictionary.DictAdd;
import com.fz.dto.system.dictionary.DictQuery;
import com.fz.dto.system.dictionary.DictUpd;
import com.fz.entity.system.Dictionary;
import com.fz.mapper.system.DictionaryMapper;
import com.fz.service.system.DictionaryService;
import com.fz.service.system.DictionaryValueService;
import com.fz.utils.JacksonUtils;
import com.fz.utils.StringUtils;
import com.fz.vo.system.dictionary.DictionaryTreeVo;
import com.fz.vo.system.dictionaryValue.DictionaryValueVo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("unchecked")
@CacheConfig(cacheNames = "dictionary")
public class DictionaryServiceImpl implements DictionaryService {
    @Resource
    private DictionaryMapper dictionaryMapper;

    @Resource
    private DictionaryValueService dictionaryValueService;

    /**
     * @方法描述: 分页查询字典列表
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/3/9
     */
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity findDictList(String desc, int pageSize, int pageNum) throws Exception {
        Page<Dictionary> page = new Page<>(pageNum, pageSize);
        IPage<Dictionary> loginLogList = dictionaryMapper.findDictList(page, desc, GlobalAll.DELETE_STATUS_NO);
        return ResponseEntity.buildSuccess(loginLogList);
    }


    /**
     * @方法描述: 插入字典
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/3/9
     */
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity saveDict(DictAdd dictAdd) throws Exception {
        Dictionary dictionary = new Dictionary();
        dictionary.setDictionaryKey(dictAdd.getDictionaryKey());
        dictionary.setDescription(dictAdd.getDescription());
        dictionaryMapper.insert(dictionary);
        return ResponseEntity.buildSuccess(null);
    }


    /**
     * @方法描述: 修改字典
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/3/9
     */
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity updateDict(DictUpd dictUpd) throws Exception {
        Dictionary dictionary = new Dictionary();
        dictionary.setId(dictUpd.getId());
        dictionary.setDictionaryKey(dictUpd.getDictionaryKey());
        dictionary.setDescription(dictUpd.getDescription());
        dictionaryMapper.updateById(dictionary);
        return ResponseEntity.buildSuccess(null);
    }


    /**
     * @方法描述: 删除字典
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/3/9
     */
    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity delDict(String ids) throws Exception {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", StringUtils.strToList(ids));
        dictionaryMapper.delete(queryWrapper);
        return ResponseEntity.buildSuccess(null);
    }

    /**
     * @param pageNum   pageNum
     * @param pageSize  pageSize
     * @param dictQuery dictQuery
     * @return com.fz.common.ResponseEntity<com.fz.vo.system.dictionary.DictionaryTreeVo>
     * @description 查询字典以及对应的字典值
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    @Cacheable
    public ResponseEntity<DictionaryTreeVo> findDictValueList(Integer pageNum, Integer pageSize, DictQuery dictQuery) {
        Page<Dictionary> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Dictionary> wrapper = new LambdaQueryWrapper<>();
        //TODO 查询条件条件添加 ->在此拼接
        wrapper.eq(Dictionary::getDeleteStatus, GlobalAll.DELETE_STATUS_NO).and(StringUtils.isNotEmpty(dictQuery.getBlurry()),
                o -> o.eq(dictQuery.getBlurry() != null, Dictionary::getDescription, dictQuery.getBlurry())
                        .or()
                        .eq(dictQuery.getBlurry() != null, Dictionary::getDictionaryKey, dictQuery.getBlurry())
        );
        List<Dictionary> dictionaries = dictionaryMapper.selectDictionaryList(page, wrapper);
        List<DictionaryTreeVo> dictionaryTreeVos = dictionaries.stream().map(dictionary -> {
            List<DictionaryValueVo> dictionaryValueVos = dictionaryValueService.selectDictionaryValueList(dictionary.getId());
            DictionaryTreeVo dictionaryTreeVo = JacksonUtils.obj2pojo(dictionary, DictionaryTreeVo.class);
            dictionaryTreeVo.setDictionaryValueCollection(dictionaryValueVos);
            return dictionaryTreeVo;
        }).collect(Collectors.toList());
        if (!dictionaryTreeVos.isEmpty()) {
            return ResponseEntity.buildSuccess(dictionaryTreeVos);
        } else {
            return ResponseEntity.buildSuccess(null);
        }
    }
}
